import { apiRequest, httpRequest } from '../manage'

// 用户设置接口
interface UserSetting {
  remindType?: string
  plateNumber?: string
  remindCount?: number
  accountCount?: number
  memoCount?: number
  oneKeyMoveUrl?: string
  homeSwipers?: string[]
}

export interface UserModel {
  id: string
  username: string
  realname: string
  avatar: string
  birthday: string
  sex: number
  email: string
  phone: string
  orgCode: string
  status: number
  delFlag: number
  workNo: string
  post: string
  telephone: string
  createBy: string
  createTime: string
  updateBy: string
  updateTime: string
  activitiSync: number
  userIdentity: number
  departIds: string
  relTenantIds: string
  clientId: string
  setting?: UserSetting
}

export const UserAPI = {
  /**
   * 用户登录
   */
  beforeWxLogin(code: string) {
    return httpRequest.post('/app/beforeLogin', {
      code,
    })
  },

  /**
   * 用户登录
   */
  login(username: string, password: string, code: string) {
    return httpRequest.post('/app/login', {
      username,
      password,
      captcha: code,
    })
  },

  /**
   * 用户退出
   */
  logout() {
    return httpRequest.post('/app/logout')
  },

  /**
   * 获取用户信息
   */
  getUserInfo() {
    return httpRequest.get('/sys/user/getUserInfo')
  },

  /**
   * 获取用户权限
   */
  getPermissions() {
    return httpRequest.get('/app/permission/')
  },

  /**
   * 修改密码
   */
  changePassword(params: {
    userId: string
    field: string
    value: string
  }) {
    return httpRequest.post('/userSetting/updateUserInfo', params)
  },

  /**
   * 重置密码
   */
  resetPassword(username: string) {
    return httpRequest.put('/sys/user/resetPassword', { username })
  },

  /**
   * 修改个人信息
   */
  updateInfo(data: Partial<UserModel>) {
    return httpRequest.put('/sys/user/updateInfo', data)
  },

  /**
   * 更新用户信息（新接口格式）
   */
  updateUserInfo(params: {
    field: string
    value: string
  }) {
    return httpRequest.post('/app/updateUserInfo', params)
  },

  /**
   * 上传头像
   */
  uploadAvatar(file: string) {
    return apiRequest.uploadFile('/app/uploadAvatar', file)
  },

  /**
   * 获取用户角色
   */
  getUserRoles(userId: string) {
    return httpRequest.get('/sys/user/userRoles', { userId })
  },

  /**
   * 升级会员
   */
  upgradeMember(params: {
    userId: string
    field: string
    value: string
  }) {
    return httpRequest.post('/userSetting/updateUserInfo', params)
  },

  /**
   * 兑换会员
   */
  redeemMember(params: {
    userId: string
    field: string
    value: string
  }) {
    return httpRequest.post('/userSetting/updateUserInfo', params)
  },

  /**
   * 注销账户
   */
  deleteAccount(params: {
    userId: string
    field: string
    value: string
  }) {
    return httpRequest.post('/userSetting/updateUserInfo', params)
  },
}
